﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ViewModelBase.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the ViewModelBase type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Windows.Common
{
    using System.ComponentModel;

    /// <summary>
    /// Defines the <seealso cref="ViewModelBase"/> type.
    /// </summary>
    public abstract class ViewModelBase : INotifyPropertyChanging, INotifyPropertyChanged
    {
        #region INotifyPropertyChanging Members

        /// <summary>
        /// Occurs when a property value is changing.
        /// </summary>
        public event PropertyChangingEventHandler PropertyChanging;

        #endregion INotifyPropertyChanging Members

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion INotifyPropertyChanged Members

        #region Administrative Properties

        /// <summary>
        /// Gets or sets a value indicating whether the view model should ignore property-change events.
        /// </summary>
        /// <value>
        /// <c>true</c> if ignore property change events; otherwise, <c>false</c>.
        /// </value>
        public virtual bool IgnorePropertyChangeEvents { get; set; }

        #endregion Administrative Properties

        #region Public Methods

        /// <summary>
        /// Raises the PropertyChanged event.
        /// </summary>
        /// <param name="propertyName">The name of the changed property.</param>
        public virtual void RaisePropertyChangedEvent(string propertyName)
        {
            if (IgnorePropertyChangeEvents)
            {
                return;
            }

            if (PropertyChanged == null)
            {
                return;
            }

            var e = new PropertyChangedEventArgs(propertyName);
            PropertyChanged(this, e);
        }

        /// <summary>
        /// Raises the PropertyChanging event.
        /// </summary>
        /// <param name="propertyName">The name of the changing property.</param>
        public virtual void RaisePropertyChangingEvent(string propertyName)
        {
            if (IgnorePropertyChangeEvents)
            {
                return;
            }

            if (PropertyChanging == null)
            {
                return;
            }

            var e = new PropertyChangingEventArgs(propertyName);
            PropertyChanging(this, e);
        }

        #endregion Public Methods
    }
}